﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class BLL_trade
    {
        /// <summary>
        /// 增
        /// </summary>
        /// <param name="td"></param>
        /// <returns></returns>
        public string insert(trade td)
        {
            if (td.trade_refrences_user_email != null)
            {
                GodHelperDBDataContext da = new GodHelperDBDataContext();
                da.trade.InsertOnSubmit(td);
                da.SubmitChanges();
                string tradeid = "" + td.trade_id;
                return tradeid;
            }
            return null;
        }

        /// <summary>
        /// 删
        /// </summary>
        /// <param name="td"></param>
        /// <returns></returns>
        public bool delete(trade td)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            var p = da.trade.Where(item => item.trade_id == td.trade_id);
            if (p.Any())
            {
                da.trade.DeleteOnSubmit(p.ToList()[0]);
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="td"></param>
        /// <returns></returns>
        public bool update(trade td)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            var p = da.trade.Where(item => item.trade_id == td.trade_id);
            if (p.Any())
            {
                var var_trade = p.ToList()[0];
                var_trade.trade_customer = td.trade_customer;
                var_trade.trade_refrences_product_name = td.trade_refrences_product_name;
                var_trade.trade_sum = td.trade_sum;
                var_trade.trade_price = td.trade_price;
                var_trade.trade_amount = td.trade_amount;
                var_trade.trade_decript = td.trade_decript;
                var_trade.trade_time = td.trade_time;
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="td"></param>
        /// <param name="refrenceskey">根据那个键值查询</param>
        /// <returns></returns>
        public List<trade> search(trade td, string refrenceskey)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            switch (refrenceskey)
            {
                case "trade_id":
                    if (td.trade_id != null)
                    {
                        var p = da.trade.Where(item => item.trade_id == td.trade_id);
                        if (p.Any())
                        {
                            return p.ToList();
                        }
                    }
                    break;
                case "trade_refrences_user_email":
                    if (td.trade_refrences_user_email != null)
                    {
                        var p = da.trade.Where(item => item.trade_refrences_user_email == td.trade_refrences_user_email);
                        if (p.Any())
                        {
                            return p.ToList();
                        }
                    }
                    break;
            }
            return null;
        }
    }
}
